package com.panfeng.xcloud.common.security.vo;

import com.panfeng.xcloud.common.core.utils.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.*;

public class SysSecurityUser implements UserDetails {

    private String userId;
    private String realName;
    private String password;
    private String username;
    private Set<ExtGrantedAuthority> authorities;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

    /** 主要提供json反序列化 **/
    public SysSecurityUser(){}

    public SysSecurityUser(String username, String password, Collection<? extends ExtGrantedAuthority> authorities, String userId) {
        this(username, password, authorities);
        this.userId = userId;
    }

    public SysSecurityUser(String userId, String username, String realName, String password, Collection<? extends ExtGrantedAuthority> authorities) {
        this(username, password, true, true, true, true, authorities);
        this.userId = userId;
        this.realName = realName;
    }

    public SysSecurityUser(String userId, String username, String realName, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends ExtGrantedAuthority> authorities) {
        this(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.userId = userId;
        this.realName = realName;
    }

    public SysSecurityUser(String username, String password, Collection<? extends ExtGrantedAuthority> authorities) {
        this(username, password, true, true, true, true, authorities);
    }

    public SysSecurityUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends ExtGrantedAuthority> authorities) {
        password = (password == null) ? "" : password;
        if (StringUtils.isNotEmpty(username)) {
            this.username = username;
            this.password = password;
            this.enabled = enabled;
            this.accountNonExpired = accountNonExpired;
            this.credentialsNonExpired = credentialsNonExpired;
            this.accountNonLocked = accountNonLocked;
            this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    private static SortedSet<ExtGrantedAuthority> sortAuthorities(Collection<? extends ExtGrantedAuthority> authorities) {
        Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
        SortedSet<ExtGrantedAuthority> sortedAuthorities = new TreeSet(new AuthorityComparator());
        Iterator var2 = authorities.iterator();
        while(var2.hasNext()) {
            ExtGrantedAuthority grantedAuthority = (ExtGrantedAuthority)var2.next();
            Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
            sortedAuthorities.add(grantedAuthority);
        }
        return sortedAuthorities;
    }

    public String getUserId() {
        return userId;
    }

    public String getRealName() {
        return realName;
    }

    @Override
    public Collection<ExtGrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAuthorities(Set<ExtGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    private static class AuthorityComparator implements Comparator<ExtGrantedAuthority>, Serializable {

        private static final long serialVersionUID = 500L;

        private AuthorityComparator() {
        }

        public int compare(ExtGrantedAuthority g1, ExtGrantedAuthority g2) {
            if (g2.getAuthority() == null) {
                return -1;
            } else {
                return g1.getAuthority() == null ? 1 : g1.getAuthority().compareTo(g2.getAuthority());
            }
        }
    }

    @Override
    public String toString() {
        return "SysSecurityUser{" +
                "userId=" + userId +
                ", realName='" + realName + '\'' +
                ", password='" + password + '\'' +
                ", username='" + username + '\'' +
                ", authorities=" + authorities +
                ", accountNonExpired=" + accountNonExpired +
                ", accountNonLocked=" + accountNonLocked +
                ", credentialsNonExpired=" + credentialsNonExpired +
                ", enabled=" + enabled +
                '}';
    }
}
